package com.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;

//@Component
//@Aspect
//@Order(5)
public class TimeLogger {

    //定义切入点
    @Pointcut("execution(* com.aop.service.*.*(..))")
    private void pointCut(){}

    //前置通知(增强)
    //@Before("pointCut()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName() + " 开始执行:");
    }

    //后置通知(增强)
    //@After("pointCut()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName() + " 执行完毕");
    }

    //环绕通知(增强)
    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        System.out.println("环绕开始1...");
        long start = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed();
//            System.out.println("返回值:" + result);
            return result;
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }finally {
            System.out.println("环绕结束1...");
            long end = System.currentTimeMillis();
            long cost = end - start;
            System.out.println("耗时:" + cost + "毫秒");
        }
    }
}
